import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Solution970 {
    public List<Integer> powerfulIntegers(int x, int y, int bound) {
        Set<Integer> set1=new HashSet<>();
        Set<Integer> set2=new HashSet<>();
        int p=1;
        while (p<bound){
            set1.add(p);
            p=p*x;
            if (x==1){
                break;
            }
        }
        p=1;
        while (p<bound){
            set2.add(p);
            p=p*y;
            if (y==1){
                break;
            }
        }
        Set<Integer> set=new HashSet<>();
        for (Integer i : set1) {
            for (Integer j : set2) {
                if (i+j<=bound){
                    set.add(i+j);
                }
            }
        }
        return new ArrayList<>(set);
    }

    public static void main(String[] args) {
        System.out.println(new Solution970().powerfulIntegers(3,5,15));
    }
}
